package xiaocao.learn.java.io.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelExample {
	
	private static final int BSIZE = 1024;
	
	public static void main(String[] args) throws Exception {
		FileChannel fc = new FileOutputStream("d://test.txt").getChannel();
		fc.write(ByteBuffer.wrap("Some txt".getBytes()));
		fc.close();
		fc = new RandomAccessFile("d://test.txt", "rw").getChannel();
		fc.position(fc.size());
		fc.write(ByteBuffer.wrap("Some more".getBytes()));
		fc.close();
		
		fc = new FileInputStream("d://test.txt").getChannel();
		ByteBuffer buff = ByteBuffer.allocate(BSIZE);
		fc.read(buff);
		buff.flip();
		while(buff.hasRemaining()) {
			System.out.println((char)buff.get());
		}
		
	}

}
